package com.chixing.controller;

import com.chixing.pojo.Mycoupons;
import com.chixing.service.MyCouponService;
import com.chixing.util.ServerResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.servlet.ModelAndView;

import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: wtl
 * @Date: 2021/10/19/16:27
 * @Description:
 */
public class MyCouponController {
    @Autowired
    private MyCouponService myCouponService;
    @GetMapping("myCoupons/{custId}")
    public ModelAndView getMyCoupon(@PathVariable("custId")Integer custId){
        ServerResponse serverResponse = myCouponService.getMyCoupons(custId);
        ModelAndView mav = new ModelAndView();
        mav.addObject("serverResponse",serverResponse);
        mav.setViewName("center/mycoupons");
        return mav;
    }

    @PostMapping("addMyCoupons")
    public ModelAndView addMyCoupons(Integer custId,Integer couponsId){
        Mycoupons mycoupons = new Mycoupons();
        mycoupons.setCouponsId(couponsId);
        mycoupons.setCustId(custId);

        Date date1 = new Date();
        Date date2 = new Date(date1.getTime() + 30*86400000);
Date date = new Date();
        mycoupons.setMycouponsEndTime(date);
        mycoupons.setStatus(0);
        ServerResponse serverResponse = myCouponService.save(mycoupons);
        ModelAndView mav = new ModelAndView();
        mav.addObject("serverResponse",serverResponse);
        mav.setViewName("center/mycoupons");
        return mav;
    }
}
